package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.manage.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import com.xyy.saas.payment.pojo.param.OpenAccountApplyParam;
import com.xyy.saas.payment.pojo.param.OpenpayParam;
import com.xyy.saas.payment.pojo.vo.*;
import com.xyy.saas.payment.service.OpenAccountService;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/openAccount")
public class OpenAccountController {

    @Autowired
    private OpenAccountService openAccountService;

    /** 获取开户状态 */
    @PostMapping(value = "/queryStatus",produces = "application/json;charset=utf-8")
    public ResultVO<OpenAccountStepStatusVO> queryStatus(@RequestBody OpenAccountStatusQueryParam param) {
        OpenAccountStepStatusVO result = null;
        try {
            result = openAccountService.queryOpenAccountStep(param);
        } catch (PaymentException e) {
            return ResultVO.createError(e.getErrMsg());
        }
        return ResultVO.createSuccess(result);
    }

    /** 获取开户信息 */
    @PostMapping(value = "/queryOpenAccountInfo",produces = "application/json;charset=utf-8")
    public ResultVO<OpenAccountInfoVO> queryOpenAccountInfo(@RequestBody OpenAccountInfoQueryParam param) {
        log.info("OpenAccountController queryOpenAccountInfo begin param:{}", param);
        OpenAccountInfoVO openAccountInfoVO = null;
        try {
            openAccountInfoVO = openAccountService.queryOpenAccountInfo(param);
        } catch (Exception e) {
            log.error("OpenAccountController#queryOpenAccountInfo error. param:{}", JSONUtils.toJSON(param), e);
            if(e instanceof PaymentException) {
                return ResultVO.createError(((PaymentException) e).getCodeEnum().getCode(), ((PaymentException) e).getErrMsg());
            }
            return ResultVO.createError(ResultCodeEnum.DEFAULT_ERROR);
        }
        return ResultVO.createSuccess(openAccountInfoVO);
    }

    /** 微信开户意愿申请重试 */
    @PostMapping(value = "/openAccountApplyment")
    public ResultVO openAccountApplyment(@RequestBody OpenAccountApplyParam param) {
        log.info("OpenAccountController#openAccountApplyment start. OpenAccountApplyParam:{}", JSONUtils.toJSON(param));
        if(StringUtils.isEmpty(param.getAccountId())) {
            if(StringUtils.isEmpty(param.getApplyCode()) && StringUtils.isEmpty(param.getThirdUserId())) {
                return ResultVO.createError(ResultCodeEnum.PARAM_ERROR);
            }
        }
        return (StringUtils.isEmpty(param.getApplyCode()) ?
                openAccountService.openAccountApplyment(param.getAccountId(), param.getThirdUserId()) :
                openAccountService.againOpenAccountApplyment(param.getApplyCode()));
    }

    @RequestMapping(value = {"/retryOpenPay"}, method = RequestMethod.POST)
    public ResultVO retryOpenPay(@RequestBody OpenpayParam param) {
        if(null == param || StringUtils.isEmpty(param.getAccountId()) || StringUtils.isEmpty(param.getChannelProduct())
            || CollectionUtils.isEmpty(param.getPowerIds())){
            return ResultVO.createError(ResultCodeEnum.PARAM_ERROR);
        }
        try {
            openAccountService.openPay(param.getAccountId(), param.getPowerIds());
        } catch (Exception e) {
            log.error("OpenAccountController#retryOpenPay error. param:{}", JSONUtils.toJSON(param), e);
            if(e instanceof PaymentException) {
                return ResultVO.createError(((PaymentException) e).getCodeEnum().getCode(), ((PaymentException) e).getErrMsg());
            }
            return ResultVO.createError(ResultCodeEnum.DEFAULT_ERROR);
        }
        return ResultVO.createSuccess();
    }

    /** 用户信息更新接口 */
    @RequestMapping(value = "/update/userinfo", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public ResultVO updateUserInfo(@RequestBody UserInfoUpdateVO param){
        log.info("OpenAccountController updateUserInfo begin UserInfoUpdateVO:{}", JSONUtils.toJSON(param));
        return openAccountService.updateUserInfo(param);
    }

}
